<?php

namespace Common\Model;

use Common\Model\XuecheModel;

class MembersModel extends XuecheModel {

    protected $pk = 'member_id';
    protected $tableName = 'members';
    protected $fields = array(
        'member_id', 'name', 'point', 'area', 'addr', 'mobile','qq_number','wx_number',
        'email', 'sex', 'reg_ip', 'regtime','disabled', 'remark', 'nickname','avatar','last_login'
    );


    /**
     * 清空数据缓存
     *
     */
    public function ClearAllCache() {

    }

    //缓存设置
    protected $_useCacheType = 'file';
    protected $_catheTime = 36000;

    public function Validate($data) {
        if (empty($data['nickname'])) {
            return false;
        }
        return true;
    }


    //获取会员列表，后台管理使用
    public function getList($where = '', $order = '', $offset = 0, $limit = 18, $fields = '') {
        $sql = "SELECT pam.login_account,mem.* FROM sdb_member_pam as pam,sdb_members as mem "
                . "WHERE {$where} AND pam.member_id = mem.member_id {$order} LIMIT {$offset},{$limit}";
        $data = $this->query($sql);
        $Pass = new \Common\Model\MemberPamModel();
        $total = $Pass->where($where)->count($this->pk);
        return array(
            'total'=>$total,
            'lists'=>$data
        );
    }

    //获取会员详细数据
    public function getDetailInfo($member_id){
        if($member_id > 0){
            $info = $this->where("`$this->pk` = '{$member_id}'")->field('point,nickname,name,email,last_login,member_id')->find();
            if(!$info['member_id']){
                return false;
            }
            $Lv = new MemberLvModel();
            $lv_info = $Lv->getMemberLv($info['point']);
            $info['lv_name'] = $lv_info['name'];
            $info['shelf_nums'] = $lv_info['shelf_nums'];
            $info['tj_nums'] = $lv_info['tj_nums'];
            $Pass = new MemberPamModel();
            $p_info = $Pass->where("`member_id`='{$member_id}'")->field('login_account')->find();
            $info['login_account'] = $p_info['login_account'];
            return $info;
        }
        return false;
    }

    public function checkRepeartMobile($mobile) {
        if (isMobile($mobile)) {
            if ($res = $this->where("`mobile` = '{$mobile}'")->find()) {
                return $res;
            } else {
                return true;
            }
        }
        return FALSE;
    }


    //会员禁用和激活操作
    public function openClose($id, $value = 0, $field = 'disabled') {
        if(parent::openClose($id, $value, $field)){
            $Pass = new MemberPamModel();
            return $Pass->openClose($id,$value);
        }
        return FALSE;
    }





}
